package edu.temple.mobile;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class SEPTripActivity extends Activity {

	private Button mPickDate;
	private int mYear;
	private int mMonth;
	private int mDay;
	static final int DATE_DIALOG_ID = 0;
	
	private Button mPickTime;
	private int mHour;
    private int mMinute;
    private int mAMPM;
    static final int TIME_DIALOG_ID = 1;
    
    private ImageButton transitViewButton;
    
    private ImageButton homeButton;
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		/** !!!!!!!!!!!!!!!ALL ICON BUTTON CODE */
		
		/**Home button */
		homeButton = (ImageButton) findViewById(R.id.actionLogo);
		homeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				Intent intent = new Intent(SEPTripActivity.this,SEPTripActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
			}
		});
        
        /**Transit view */
        transitViewButton = (ImageButton) findViewById(R.id.transitview);
        
        transitViewButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(SEPTripActivity.this,
						TransitViewActivityRouteSelectorActivity.class);
				startActivity(intent);
			}
		});
        

		/** Date method */
			mPickDate = (Button) findViewById(R.id.depArrDate);
			// add a click listener to the button
			mPickDate.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					showDialog(DATE_DIALOG_ID);
				}
			});
	
			final Calendar c = Calendar.getInstance();
			mYear = c.get(Calendar.YEAR);
			mMonth = c.get(Calendar.MONTH);
			mDay = c.get(Calendar.DAY_OF_MONTH);
		/** Date method */
			
		/** Time Picker */
			mPickTime = (Button) findViewById(R.id.depArrTime);
			// add a click listener to the button
	        mPickTime.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(TIME_DIALOG_ID);
	            }
	        });

	        // get the current time
	        mHour = c.get(Calendar.HOUR_OF_DAY);
	        mMinute = c.get(Calendar.MINUTE);
	        mAMPM = c.get(Calendar.AM_PM);

	        // display the current date
	        updateTime();

	}
	
	/** Time and Date methods */
		 // the callback received when the user "sets" the date in the dialog
	    private DatePickerDialog.OnDateSetListener mDateSetListener =
	            new DatePickerDialog.OnDateSetListener() {
	
	                public void onDateSet(DatePicker view, int year, 
	                                      int monthOfYear, int dayOfMonth) {
	                    mYear = year;
	                    mMonth = monthOfYear;
	                    mDay = dayOfMonth;
	                    updateTime();
	                }
	            };
	
		// updates the date in the TextView
		private void updateTime() {
			mPickDate.setText(new StringBuilder()
					// Month is 0 based so add 1
					.append(mMonth + 1).append("/").append(mDay).append("/")
					.append(mYear).append(" "));
			mPickTime.setText(
			        new StringBuilder()
			                .append(pad(mHour)).append(":")
			                .append(pad(mMinute)));
		}
		
		private static String pad(int c) {
		    if (c >= 10)
		        return String.valueOf(c);
		    else
		        return "0" + String.valueOf(c);
		}
		
		//Add 5 minutes to current time
		private static String padM(int c) {
		    switch(c){
		    case 55: c = 0;
		    case 56: c = 1;
		    case 57: c = 2;
		    case 58: c = 3;
		    case 59: c = 4;
		    default: c = c+5;
		    }
			
			return pad(c);
		}
		
		// the callback received when the user "sets" the time in the dialog
		private TimePickerDialog.OnTimeSetListener mTimeSetListener =
		    new TimePickerDialog.OnTimeSetListener() {
		        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		            mHour = hourOfDay;
		            mMinute = minute;
		            updateTime();
		        }
		    };
		
		@Override
		protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case DATE_DIALOG_ID:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
	    case TIME_DIALOG_ID:
	        return new TimePickerDialog(this,
	                mTimeSetListener, mHour, mMinute, false);

	    }
	    return null;
	}
}